package com.shenmazong.servereduuser.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shenmazong.servereduuser.entity.TbStaff;
import com.shenmazong.servereduuser.mapper.TbStaffMapper;
import com.shenmazong.servereduuser.service.TbStaffService;
import com.shenmazong.utils.ResultResponse;
import com.shenmazong.vo.BatchChooseIdVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 */
@Service
public class TbStaffServiceImpl implements TbStaffService {
    @Autowired
    private TbStaffMapper staffMapper;

    @Override
    public ResultResponse deleteStall(BatchChooseIdVo idVo) {
        if(idVo.getIds().size()<=0){
            return ResultResponse.FAILED(400,"请至少选择一名员工");
        }

        try {
            staffMapper.deleteBatchIds(idVo.getIds());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED(404,"删除失败");
        }
        return ResultResponse.SUCCESS(200,"删除成功");
    }

    @Override
    public ResultResponse addStall(TbStaff tbStaff) {
        TbStaff one = staffMapper.selectOne(
                new QueryWrapper<TbStaff>().lambda()
                        .eq(TbStaff::getStaffName, tbStaff.getStaffName())
                        .last("limit 1")
        );
        if(one != null) {
            return ResultResponse.FAILED(400, "用户名重名");
        }

        staffMapper.insert(tbStaff);

        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse updateStall(TbStaff staff) {
        if(staff.getStaffId() == null || staff.getStaffId() == 0) {
            return ResultResponse.FAILED(400, "用户ID不能为空");
        }

        TbStaff one = staffMapper.selectById(staff.getStaffId());
        if(one == null) {
            return ResultResponse.FAILED(404, "用户不存在");
        }

        try {
            staffMapper.updateById(staff);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED(500,"删除失败");
        }
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse getOndeStaff(TbStaff staff) {

        if(staff.getStaffId() == null || staff.getStaffId() == 0) {
            return ResultResponse.FAILED(400, "用户ID不能为空");
        }

        TbStaff one = staffMapper.selectById(staff.getStaffId());
        if(one == null) {
            return ResultResponse.FAILED(404, "用户不存在");
        }

        one.setStaffPass("");
        one.setStaffSalt("");

        return ResultResponse.SUCCESS(one);
    }
}




